#ifndef COMMON_QUERY_HELPER_H
#define COMMON_QUERY_HELPER_H

#include <ostream>
#include <string>
#include <unordered_map>
#include <vector>

namespace Common
{

class QueryHelper final
{
public:
    explicit QueryHelper(const std::string& query);

    std::string get(
        const std::string& key,
        const std::string& defaultValue = std::string()
    ) const;

    bool getBoolean(
        const std::string& key,
        const bool defaultValue = false
    ) const;

    int getInteger(
        const std::string& key,
        const int defaultValue = 0
    ) const;

    std::vector<std::string> keys() const;

    std::unordered_map<std::string, std::string> data() const;

    std::string query() const;

private:
    void split(
        const std::string& value,
        char delimiter,
        std::vector<std::string>& elements
    ) const;

    void decodeUrl(const std::string& in, std::string& out) const;

    std::unordered_map<std::string, std::string> parse(
        const std::string& requestBody
    ) const;

private:
    const std::string m_query;
    const std::unordered_map<std::string, std::string> m_data;
};

std::unordered_map<std::string, std::string> queryMap(const std::string& query);

std::ostream& operator<<(std::ostream& out, const QueryHelper& helper);

} // namespace Common

#endif // COMMON_QUERY_HELPER_H
